const path = require("path")
const { whenProd, getPlugin, pluginByName } = require("@craco/craco")
const AntdDayjsWebpackPlugin = require("antd-dayjs-webpack-plugin") // 插件

module.exports = {
  webpack: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
    plugins: {
      add: [new AntdDayjsWebpackPlugin()], // 加入dayjs插件 替换moment dayjs相对moment要小一些
    },
    configure: (webpackConfig) => {
      // 修改webpack配置 只会发生在生产环境
      whenProd(() => {
        webpackConfig.externals = {
          react: "React",
          "react-dom": "ReactDOM",
          redux: "Redux",
        }
        const { isFound, match } = getPlugin(
          webpackConfig,
          pluginByName("HtmlWebpackPlugin")
        )
        if (isFound) {
          match.userOptions.cdn = {
            js: [
              "https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.production.min.js",
              "https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js",
              "https://cdn.bootcdn.net/ajax/libs/redux/4.1.0/redux.min.js",
            ],
          }
        }
      })
      return webpackConfig
    },
  },
}
